<?php

namespace App\Console\Commands;

use App\Services\TTSService;
use Illuminate\Console\Command;
use TencentCloud\Common\CommonClient;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Exception\TencentCloudSDKException;

class QCloudTts extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:qcloud-tts';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        try {
            $text = "你好！我是OpenAI助手，很高兴为您提供帮助。请问您需要什么帮助呢？";
            $url = TTSService::textToAudio($text);
            $this->info($url);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    public function textToVoice()
    {
        try {
            $cred = new Credential(getenv("QCLOUD_SECRET_ID"), getenv("QCLOUD_SECRET_KEY"));
            $client = new CommonClient("tts", "2019-08-23", $cred, "ap-guangzhou");
            // $sessionId = "session-".Str::random(20);
            $headers = array();
            $action = "CreateTtsTask";
            $body = [
                'Text' => "你好！我是OpenAI助手，很高兴为您提供帮助。请问您需要什么帮助呢？",
                'ModelType' => 1,
            ];
            $resp = $client->callJson(
                $action,
                $body,
                $headers,
            );
            dd($resp);
        } catch (TencentCloudSDKException $e) {
            $this->error($e->getMessage());
        }
    }

    public function describeTtsTaskStatus()
    {
        try {
            $cred = new Credential(getenv("QCLOUD_SECRET_ID"), getenv("QCLOUD_SECRET_KEY"));
            $client = new CommonClient("tts", "2019-08-23", $cred, "ap-guangzhou");
            // $sessionId = "session-".Str::random(20);
            $headers = array();
            $action = "DescribeTtsTaskStatus";
            $body = [
                "TaskId" => "gz-cd721fc9-3f5e-44f9-a04f-de7dce4fcd7b"
            ];
            $resp = $client->callJson(
                $action,
                $body,
                $headers,
            );
            dd($resp);
        } catch (TencentCloudSDKException $e) {
            $this->error($e->getMessage());
        }
    }
}
